package com.etang.a2020a1018.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.etang.a2020a1018.tools.AppConfig;

import java.util.Calendar;

public class My_Ill_View extends View {

    Paint paint = new Paint();// 画笔
    int height = 0;// 高
    int width = 0;// 宽
    int radius = 0;// 圆心
    Canvas canvas;// 画布


    public My_Ill_View(Context context) {
        super(context);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {

                    }
                    invalidate();
                }
            }
        }).start();
    }

    public My_Ill_View(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public My_Ill_View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas = canvas;// 赋值，无所谓的操作，方便查看而已
        super.onDraw(canvas);
        width = canvas.getWidth() / 2;// 获取半径宽
        height = canvas.getHeight() / 2;// 获取半径高
        radius = height - 3;// 获取半径
        paint.setColor(Color.BLACK);// 设置画笔颜色
        paint.setStyle(Paint.Style.STROKE);// 设置画笔风格
        paint.setStrokeWidth(3);// 设置画笔粗细
        paint.setAntiAlias(true);// 设置抗锯齿
        // 绘制外圆
        canvas.drawCircle(width, height, radius, paint);
        // 循环绘制小刻度（分钟\秒）
        for (int i = 0; i < 60; i++) {
            canvas.save();
            canvas.rotate(i * 6, width, height);
            canvas.drawLine(width, height - radius, width,
                    height - radius + 10, paint);
            canvas.restore();
        }
        // 循环绘制大刻度（小时）
        for (int i = 0; i < 12; i++) {
            canvas.save();
            canvas.rotate(i * 30, width, height);
            canvas.drawLine(width, height - radius, width,
                    height - radius + 20, paint);
            canvas.restore();
        }
        // 设置画笔粗细
        paint.setStrokeWidth(15);
        // 设置画笔风格
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(5);// 设置画笔粗细
        canvas.rotate(AppConfig.ill * 6, width, height);
        canvas.drawLine(width, height, width, height - 60, paint);
        // 绘制小圆心
        canvas.drawCircle(width, height, 15, paint);
        canvas.drawText("光照：" + AppConfig.temp, 0, height + 30, paint);
    }
}
